
<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script type="text/javascript" src="../js/jquery-1.8.3.js"></script>
		<script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../js/easyui/locale/easyui-lang-zh_CN.js"></script>
		<link rel="stylesheet" href="../js/easyui/themes/default/easyui.css" />
		<link rel="stylesheet" href="../js/easyui/themes/icon.css" />
		<title>猜数小游戏</title>
		<script type="text/javascript">
			var count = 1; //竞猜的次数
			var guess_num = -1; //竞猜的数字
			var randNum = parseInt(Math.random()*100); //生成随机数
			$(function() {
				guessNum();
			});
            
			var beginTime;
			var endTime;
			function guessNum() {
				$.messager.prompt("第[" + count + "]次竞猜", "请输入你竞猜的数字（1~100）：", function(b) {
					if(count == 1){//只在第一次竞猜时，认为是游戏的开始时间
						beginTime = new Date();//竞猜起始时间
					}
					guess_num = b;
					if(guess_num > randNum) {
						$.messager.confirm("提示", "猜大啦，还差一点，是否继续竞猜？",function(result){
							if(result){
								guessNum();
							}
						});
					} else if(guess_num < randNum) {
						$.messager.confirm("提示", "猜小啦，还差一点，是否继续竞猜？",function(result){
							if(result){
								guessNum();
							}
						});
					} else {
						$.messager.alert("提示", "恭喜，猜对了，你好棒！");
						//竞猜正确后，将结束游戏，此时，记录游戏的结束时间
						endTime = new Date();
						showResult();
						return;
					}
					
					count++;
				});
			}
			
			function showResult(){
				var result = "您总共竞猜了["+count+"]轮，总共耗时为["+(endTime-beginTime)/1000+"]秒，成绩不错哦！";
				$("#result").html(result);
				$.messager.show({
					title:"游戏结果统计",
					msg:result,
					timeout:10000,
					showType:'slide'
				});
			}
		</script>
	</head>

	<body>
		<h1><div id="result"></div></h1>
	</body>

</html>